# Scenario : encode Wave PCM files to Vorbis
# Copyright 2007-2011, Sylvain Rougeaux (Cyber Sinh), www.luminescence-software.org
# Version 2.3 (2011-09-24)

#################################################################
$source = "%SOURCE%\*"
$recursive = %RECURSIVE%
$input_file = "%INPUT_FILENAME_PATTERN%"
$input_folder = "%INPUT_FOLDER_PATTERN%"
$output_file = "%OUTPUT_FILENAME_PATTERN%"
$output_folder = "%OUTPUT_FOLDER_PATTERN%"
$options = "--quality %QUALITY%"
$max_encoding = %MAX_ENCODING%
$priority = "%PRIORITY%"
$shutdown = %SHUTDOWN%
$hibernate = %HIBERNATE%
#################################################################

Clear-Host

$bin = Split-Path $MyInvocation.MyCommand.Definition -Parent

[Reflection.Assembly]::LoadFrom("$bin\Functions.dll") | Out-Null

$encoder = "oggenc2"
$oggenc2 = "$bin\Encoders\$encoder.exe"

if ($recursive) { $files = Get-ChildItem $source -Include *.wav -Recurse }
else { $files = Get-ChildItem $source -Include *.wav }

if ($files -eq $null)
{
   Write-Host "There is no file to process."
   exit
}

if ($files.GetType().Name -eq "FileInfo") { $count = 1 }
else { $count = $files.Length }

$i = 0

foreach ($file in $files)
{
   $i++
   Write-Progress -Activity "Encoding Wave PCM files to Vorbis ($i / $count)..." -Status "$file" -PercentComplete ($i / $count * 100)
   
   $tags = [XiphMediaEncoder.TagProcessor]::TagFromFilename($null, $input_file, $file)
   $tags = [XiphMediaEncoder.TagProcessor]::TagFromPath($tags, $input_folder, $file)
   
   if (![String]::IsNullOrEmpty($output_folder))
   {
      $folder = [XiphMediaEncoder.TagProcessor]::PathFromTags($output_folder, $tags)
      if (![System.IO.Directory]::Exists($folder)) { [System.IO.Directory]::CreateDirectory($folder) | Out-Null }
   }
   else
   {
      $folder = [System.IO.Path]::GetDirectoryName($file)
   }
   
   $filename = [System.IO.Path]::GetFileNameWithoutExtension($file) + ".ogg"
   if (![String]::IsNullOrEmpty($output_file))
   {
      $buffer = [XiphMediaEncoder.TagProcessor]::FileNameFromTags($output_file, $tags)
      if ($buffer -ne $null) { $filename = $buffer + ".ogg" }
   }
   
   $args_tags = ""
   for ($j = 0; $j -lt $tags.Count; $j++)
   {
      foreach ($value in $tags.Values[$j])
      {
         $args_tags += "--comment=""$($tags.Keys[$j])=$value"" "
      }
   }
   
   $output = [System.IO.Path]::Combine($folder, $filename)
   
   $startInfo = New-Object -TypeName Diagnostics.ProcessStartInfo
   $startInfo.FileName = $oggenc2
   $startInfo.Arguments="--quiet $options $args_tags --output=""$output"" ""$file"""   
   $startInfo.UseShellExecute = $false
   $startInfo.RedirectStandardOutput = $true
       
   $proc = [System.Diagnostics.Process]::Start($startInfo)
   $proc.PriorityClass = $priority

   $nb_instance = (Get-Process | Where-Object { $_.ProcessName -eq $encoder }).Length
   while ($nb_instance -ge $max_encoding)
   {
      Start-Sleep -Milliseconds 500
      $nb_instance = (Get-Process | Where-Object { $_.ProcessName -eq $encoder }).Length
   }
}

Write-Host "Encoding completes."

if ($shutdown) 
{ 
    shutdown /s /t 3
    exit 
}

if ($hibernate) 
{ 
    shutdown /h /t 3 
    exit
}